var cartObj;

function updateInformaion(obj){
    $.ajax({
        type:"GET",
        url:"/information/update",
        data:{method:"updateInformation",username:$('#username').val(),gender:$("input[name='gender']").val(),phone:$('#phone').val(),email:$('#email').val()},
        dataType:"json",
        success:function(data){
            console.log(data);
            if(data.updateResult == "true"){
                cancleBtn();
                obj.parent().parent().parent().parent("div").remove();
                window.location.reload();
            }else if(data.updateResult == "false"){
                changeDLGContent("对不起，更改用户个人信息失败");
            }else if(data.updateResult == "notexist"){
                changeDLGContent("对不起，用户不存在");
            }
        },
        error:function(data){
            changeDLGContent("对不起，更改失败");
        }
    });
}

function selectOrderItems(){
    $.ajax({
        type:"POST",
        url:"/information/selectOrder",
        data:{method:"selectOrder"},
        dataType:"json",
        async:false,
        success: function (data) {
            var orderList = data.orderList;
            console.log(data);
            $.each(orderList,function (i,orderItem) {
                $("#orderShow").append("<div class=\"row orderItem\"><div class='col-md-12'><div class='basic'><div class='orderItem'>" +
                    "<li><span>"+orderItem.serialnumber+"</span></li>" +
                    // "<li><span>"+orderItem.creattime+"</span></li>" +
                    "<li><span>"+"￥"+orderItem.cost+"</span>" +
                    "</li><a href='javascript:;' class='showDetail' orderId = " + orderItem.id + ">Details</a></div></div></div></div>");
            });
            $("#order").show();
        }
    });
}

function selectOrderDetails(obj){
    $.ajax({
        type:"POST",
        url:"/information/selectOrderDetail",
        data:{method:"selectOrderDetail",orderId:obj.attr("orderId")},
        dataType:"json",
        async:false,
        success: function (data) {
            var orderDetailList = data.orderDetailList;
            console.log(data);
            var content = "<div class=\"row detailItem\">\n" +
                "\t\t\t<div class=\"col-md-12\">\n" +
                "\t\t\t\t<div class=\"details\">\n" +
                "\t\t\t\t\t<li><label>phonename:</label></li>\n" +
                "\t\t\t\t\t<li><label>quantity:</label></li>\n" +
                "\t\t\t\t\t<li><label>cost:</label></li>\n" +
                "\t\t\t\t</div>\n" +
                "\t\t\t</div>\n" +
                "\t\t</div>";
            $.each(orderDetailList,function (i,orderDetailItem) {
                content = content + "<div class=\"row detailItem\">\n" +
                    "\t\t\t<div class=\"col-md-12\">\n" +
                    "\t\t\t\t<div class=\"details\">\n" +
                    "\t\t\t\t\t<li><span>"+orderDetailItem.phonename+"</span></li>\n" +
                    "\t\t\t\t\t<li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+orderDetailItem.quantity+"</span></li>\n" +
                    "\t\t\t\t\t<li><span>&nbsp;&nbsp;&nbsp;&nbsp;"+"￥"+orderDetailItem.cost+"</span></li>\n" +
                    "\t\t\t\t</div>\n" +
                    "\t\t\t</div>\n" +
                    "\t\t</div>";
            });
            obj.append(content);
        }
    });
}

function openYesOrNoDLG(){
    $('.zhezhao').css('display', 'block');
    $('#updateInformation').fadeIn();
}

function cancleBtn(){
    $('.zhezhao').css('display', 'none');
    $('#updateInformation').fadeOut();
}

function changeDLGContent(contentStr){
    var p = $(".updateInformationMain").find("p");
    p.html(contentStr);
}

$(function(){
    $('#no').click(function () {
        cancleBtn();
    });

    $('#yes').click(function () {
        updateInformaion(cartObj);
    });

    $(".updateInfo").on("click",function(){
        cartObj = $(this);
        changeDLGContent("你确定更改用户的个人信息吗？");
        openYesOrNoDLG();
    });

    $(".showOrder").on("click",function(){
        $(".orderItem").remove();
        selectOrderItems();
    });

    $(document).on("click",'.showDetail',function(){
        $(".detailItem").remove();
        cartObj = $(this);
        selectOrderDetails(cartObj);
    });
});